mtk5 / autoclicker [Idle Game Maker]

// ==UserScript==
// @name         autoclicker [Idle Game Maker]
// @namespace    http://tampermonkey.net/
// @version      0.0.1
// @description  autoclick for games created with Idle Game Maker
// @author       mtk5
// @license      MIT
// @match        http://orteil.dashnet.org/igm/*
// @match        http://orteil.dashnet.org/igm/?g=*
// @match        http://orteil.dashnet.org/igm/index.html?g=*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var autoclick = true;
    var autobuy_buildings = false;
    var autobuy_upgrades = false;
    function keyup(e) {
        if(e.key == '0') {
            autoclick = !autoclick;
            autobuy_buildings = !autobuy_buildings;
            autobuy_upgrades = !autobuy_upgrades;
        }
        if(e.key == '1') {
            autoclick = !autoclick;
        }
        if(e.key == '2') {
            autobuy_buildings = !autobuy_buildings;
        }
        if(e.key == '3') {
            autobuy_upgrades = !autobuy_upgrades;
        }
    }

    setTimeout(function() {
        var gpl = document.body.querySelector('[src="player.html"]');
        if(gpl) {
            var iframeDocument = gpl.contentDocument || gpl.contentWindow.document;
            if(iframeDocument && iframeDocument.body) {
                var ifb = iframeDocument.body;
                ifb.addEventListener('keyup', keyup, false);

                var box = ifb.getElementsByClassName('box-things');
                var builds = box[2];
                var ups = box[3];
                setInterval(function() { if(autoclick == true && ifb.getElementsByClassName("thing button") != null && ifb.getElementsByClassName("thing button")[0] != null) { ifb.getElementsByClassName("thing button")[0].click(); } }, 0.1);
                setInterval(function() {
                    if(autoclick === true) {
                        if(ifb.getElementsByClassName("thing shiny bigButton noText") != null && ifb.getElementsByClassName("thing shiny bigButton noText")[0] != null) {
                            ifb.getElementsByClassName("thing shiny bigButton noText")[0].click();
                        }
                        if(ifb.getElementsByClassName("thing shiny noText") != null && ifb.getElementsByClassName("thing shiny noText")[0] != null && ifb.getElementsByClassName("thing shiny noText")[0].className.includes('noClick') == false) {
                            ifb.getElementsByClassName("thing shiny noText")[0].click();
                            console.log("shiny clicked");
                        }
                    }
                    if(autobuy_buildings === true && builds !== null && builds.childNodes != null) {
                        builds.childNodes.forEach(function(e) {
                            if(e.className.includes("cantAfford") == false) {
                                e.click();
                            }
                        });
                    }
                    if(autobuy_upgrades == true && ups !== null && ups.childNodes != null) {
                        ups.childNodes.forEach(function(e) {
                            if(!e.className.includes("cantAfford") && e.className.includes('notOwned') == true) {
                                e.click();
                            }
                        });
                    }
                }, 1000);
            }
        } else {
            console.log('game not found.');
        }
    }, 2500);
})();